package com.bling.common.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 时间日期工具
 * @author BLingSoft
 *
 */
public class TimeUtil {

	public final static String DATE = "yyyy-MM-dd";
	public final static String TIME = "HH:mm:ss";
	public final static String DATE_TIME = "yyyy-MM-dd HH:mm:ss";
	private final static int ONE_DAY = 1000*60*60*24; // 一天的毫秒数
	/**
	 * 获取当前长数型时间
	 * @return
	 */
	public static long getNowLongTime(){
		return System.currentTimeMillis();
	}
	/**
	 * 获取当前整数型时间
	 * @return
	 */
	public static int getNowIntTime(){
		return CommonTypeChange.getIntFromLong(System.currentTimeMillis());
	}
	/**
	 * 获取当前字符串类型日期
	 * @return
	 */
	public static String getNowStringTime(){
		return String.valueOf(System.currentTimeMillis());
	}
	/**
	 * 格式获取日期字符串
	 * @param format
	 * @return
	 */
	public static String getNowStringFromDate(String format){
		return new SimpleDateFormat(format).format(new Date());
	}
	/**
	 * 日期字符串转换为Date
	 * @param format
	 * @param timeStr
	 * @return
	 */
	public static Date getDateFromString(String format,String timeStr){
		Date date = null;
		try {
			date =  new SimpleDateFormat(format).parse(timeStr);
		} catch (ParseException e) {}
		return date;
	}
	/**
	 * 计算两个日期之间的天数
	 * @param oldDate
	 * @param newDate
	 * @param format
	 * @return
	 */
	public static int getDistanceDay(String oldDate,String newDate,String format){
		long oldTime = TimeUtil.getDateFromString(format,oldDate).getTime();
		long newTime = TimeUtil.getDateFromString(format,newDate).getTime();
		return CommonTypeChange.getIntFromLong((newTime-oldTime)/ONE_DAY);
	}
	/**
	 * 返回某个日期与现在之间天数，负数为之前，正数为之后
	 * @param date
	 * @param format
	 * @return
	 */
	public static int getDistanceFromNow(String date,String format){
		return TimeUtil.getDistanceDay(TimeUtil.getNowStringFromDate(format),date,format);
	}
}
